package com.funky.common.util;


import com.auth0.jwt.JWT;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JWTutil {

  // 开发环境设置过期时间
  public long EXPIRE_TIME = 2*60*1000;

  /**
   * 通过header头部的token得到id
   * @return token中包含的id
   */
  public static String getId(String token) {
    System.err.println(  "Integer getId入参：" + token);
    try{
//      userId = JWT.decode(token).getAudience().get(0);
      DecodedJWT jwt = JWT.decode(token);
//      System.err.println(  "Integer getId出参：" + jwt.getClaim("id"));
      System.err.println(  "Integer getId出参：" + jwt.getAudience().get(0));
      return jwt.getAudience().get(0);
    } catch (JWTDecodeException e) {
      System.err.println(  "Integer getId出参===========" +
              "sss" +
              "：" );
      return null;
    }
  }

  /**
   * 通过header头部的token得到login_name
   * @return token中包含的用户名
   */
  public static String getUserName(String token) {
    try {
      DecodedJWT jwt = JWT.decode(token);
      System.out.println("jwt解析的name = " + jwt.getClaim("user_name").asString());
      return jwt.getClaim("user_name").asString();
    } catch (JWTDecodeException e) {
      System.out.println("解析name失败");
      return null;
    }
  }
}